package lily.com.app;

import android.graphics.PixelFormat;
import android.graphics.Point;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

//TODO mCoordOffset may be used to adjust row display
public class TaskListViewGestureResponder implements GestureResponder {

	private View dragView = null;
	private WindowManager.LayoutParams mWindowParams = null;
	private WindowManager mWindowManager = null;
	private int dragFromPos = -1;

	@Override
	public void onClick(View v, Point event) {
//		Log.d("DEBUG", "TaskListViewGestureResponder onClick");
	}

	@Override
	public void startDrag(View v, Point fromEvent, Point toEvent) {
		ListView listView = (ListView) v;
//		Log.d("DEBUG", "TaskListViewGestureResponder startDrag");
		int x = fromEvent.x;
		int y = fromEvent.y;
		int position = listView.pointToPosition(x, y);
		if (position == AbsListView.INVALID_POSITION)
			return;
		else{
			dragFromPos = position;
		}
		View row = listView.getChildAt(position
				- listView.getFirstVisiblePosition());
		
		//TODO for debugger, to delete
		int mCoordOffset = 0;
		Log.d("DEBUG", "position = " + position);
		if (null == row) {
//			Log.d("DEBUG", "null row");
		}
//		Log.d("DEBUG", "row id = " + row.getId());
		
		TextView txtRecord = (TextView) row.findViewById(R.id.txtRecord);
		String title = txtRecord.getText().toString();
		
		LayoutInflater inflator = LayoutInflater.from(listView.getContext());
		dragView = inflator.inflate(R.layout.list_item, null);
		txtRecord = (TextView) dragView.findViewById(R.id.txtRecord);
		txtRecord.setText(title);
		txtRecord.setBackgroundColor(0xFF33B5E5);

		mWindowParams = new WindowManager.LayoutParams();
		mWindowParams.gravity = Gravity.TOP;
		mWindowParams.x = 30;
		mWindowParams.y = row.getTop() + mCoordOffset - 10;

		mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
		mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
		mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
				| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
				| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
				| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
		mWindowParams.format = PixelFormat.TRANSLUCENT;
		mWindowParams.windowAnimations = 0;

		mWindowManager = (WindowManager) listView.getContext()
				.getSystemService("window");
		mWindowManager.addView(dragView, mWindowParams);
	}

	@Override
	public void onDrag(View v, Point fromEvent, Point toEvent) {
//		Log.d("DEBUG", "TaskListViewGestureResponder onDrag");
		
		ListView listView = (ListView) v;
		int toX = toEvent.x;
		int toY = toEvent.y;
		
		if (null == dragView) {
//			Log.e("DEBUG", "dragView is null");
			return;
		}
		if (null == mWindowManager) {
//			Log.e("DEBUG", "mWindowManager is null");
			return;
		}
		if (null == mWindowParams) {
//			Log.e("DEBUG", "mWindowParams is null");
			return;
		}
		
		int dragToPos = listView.pointToPosition(toX, toY);
		if (dragToPos == AbsListView.INVALID_POSITION)
			return;
		
		int offPosToBottom = listView.getLastVisiblePosition() - dragToPos;
		int offPosToTop = dragToPos - listView.getFirstVisiblePosition();
		if(offPosToBottom  <= 2){
			listView.smoothScrollToPosition(listView.getLastVisiblePosition() + offPosToBottom);
		}
		if(offPosToTop <= 2){
			listView.smoothScrollToPosition(listView.getFirstVisiblePosition() - offPosToTop);
		}
		
		View row = listView.getChildAt(dragToPos
				- listView.getFirstVisiblePosition());
		int mDragOffset = toY - row.getTop();
		int mCoordOffset = 0;
		
		//TODO it seems mWindowParams.x = 10; take no work
		mWindowParams.x = 10;
		mWindowParams.y = toY - mDragOffset + mCoordOffset;
		mWindowManager.updateViewLayout(dragView, mWindowParams);
		
		if(dragFromPos != dragToPos){
			TaskListViewAdapter adapter = TaskListViewAdapter.getInstance(listView.getContext());
//			Log.d("DEBUG", "moveTask from " + dragFromPos + " to " + dragToPos);
			adapter.moveTask(dragFromPos, dragToPos);
			dragFromPos = dragToPos;
		}
		
		
	}

	@Override
	public void stopDrag(View v, Point fromEvent, Point toEvent) {
//		Log.d("DEBUG", "TaskListViewGestureResponder stopDrag");
		if (dragView != null) {
			mWindowManager.removeView(dragView);
			dragView = null;
		}

	}

	@Override
	public void onZoomIn(View v, Point[] fromEvent, Point[] toEvent) {
		
	}

	@Override
	public void onZoomOut(View v, Point[] fromEvent, Point[] toEvent) {
//		Log.d("DEBUG", "TaskListViewGestureResponder onZoomIn");
	}

	@Override
	public void startZoomIn(View v, Point[] fromEvent, Point[] toEvent) {
//		Log.d("DEBUG", "TaskListViewGestureResponder startZoomIn");
		ListView listView = (ListView) v;
		int pos1 = listView.pointToPosition(fromEvent[0].x, fromEvent[0].y);
		int pos2 = listView.pointToPosition(fromEvent[1].x, fromEvent[1].y);
//		Log.d("DEBUG", "pos1 = " + pos1);
//		Log.d("DEBUG", "pos2 = " + pos2);
		int posTemp = 0;
		if (pos1 == AbsListView.INVALID_POSITION
				|| pos2 == AbsListView.INVALID_POSITION)
			return;

		// place the minor one in pos1
		if (pos1 > pos2) {
			posTemp = pos1;
			pos1 = pos2;
			pos2 = posTemp;
		}

		// on most occasions in multi-touch pointer mode, the points tends to
		// float toward y positive
		// we use position adjustment to avoid point float in device experiments
		if (pos1 == pos2)
			pos1 = pos2 - 1;
		if (pos2 - pos1 == 1) {
			Log.d("DEBUG", "trigger insertItem");
			
//			View row1 = listView.getChildAt(pos1
//					- listView.getFirstVisiblePosition());
//			View row2 = listView.getChildAt(pos2
//					- listView.getFirstVisiblePosition());
//			TextView txt1 = (TextView) row1.findViewById(R.id.txtRecord);
//			txt1.setBackgroundColor(Color.GRAY);
//			TextView txt2 = (TextView) row2.findViewById(R.id.txtRecord);
//			txt2.setBackgroundColor(Color.GRAY);
			
			TaskListViewAdapter listAdapter = (TaskListViewAdapter)listView.getAdapter();
			listAdapter.insertItem(pos2);
			listAdapter.notifyDataSetChanged();
		}
		
	}

	@Override
	public void startZoomOut(View v, Point[] fromEvent, Point[] toEvent) {}

	@Override
	public void startSwipeRight(View v, Point fromEvent, Point toEvent) {
//		Log.d("DEBUG", "TaskListViewGestureResponder startSwipeRight");
		ListView listView = (ListView) v;
		TaskListViewAdapter apdapter = (TaskListViewAdapter)listView.getAdapter();
		int position = listView.pointToPosition(fromEvent.x, fromEvent.y);
		if (position == AbsListView.INVALID_POSITION)
			return;
		View row = listView.getChildAt(position
					- listView.getFirstVisiblePosition());
		TaskDomain task = (TaskDomain)apdapter.getItem(position);
		int uiStatus = task.getUiStatus();
		Button btnDelete = (Button) row.findViewById(R.id.btnDelete);
		Button btnFinish = (Button) row.findViewById(R.id.btnFinish);
		TextView txtRecord = (TextView) row.findViewById(R.id.txtRecord);
//		Log.d("DEBUG", "uiStatus = " + uiStatus);
		switch(uiStatus){
		case TaskDomain.TaskUIStatus.NONE:
//			Log.d("DEBUG", "uiStatus = NONE");
			btnDelete.setVisibility(View.GONE);
			btnFinish.setVisibility(View.VISIBLE);
			txtRecord.setBackgroundColor(0xFF33B5E5);
			task.setUiStatus(TaskDomain.TaskUIStatus.FINISH);
			break;
		case TaskDomain.TaskUIStatus.FINISH:
//			Log.d("DEBUG", "uiStatus = FINISH");
			break;
		case TaskDomain.TaskUIStatus.DELETE:
//			Log.d("DEBUG", "uiStatus = DELETE");
			btnDelete.setVisibility(View.GONE);
			btnFinish.setVisibility(View.GONE);
			task.setUiStatus(TaskDomain.TaskUIStatus.NONE);
			break;
		}
	}

	@Override
	public void startSwipeLeft(View v, Point fromEvent, Point toEvent) {
//		Log.d("DEBUG", "TaskListViewGestureResponder startSwipeRight");
		ListView listView = (ListView) v;
		TaskListViewAdapter apdapter = (TaskListViewAdapter)listView.getAdapter();
		int position = listView.pointToPosition(fromEvent.x, fromEvent.y);
		if (position == AbsListView.INVALID_POSITION){
//			Log.d("DEBUG", "INVALID_POSITION");
			return;
		}
		View row = listView.getChildAt(position
					- listView.getFirstVisiblePosition());
		TaskDomain task = (TaskDomain)apdapter.getItem(position);
		int uiStatus = task.getUiStatus();
		Button btnDelete = (Button) row.findViewById(R.id.btnDelete);
		Button btnFinish = (Button) row.findViewById(R.id.btnFinish);
		TextView txtRecord = (TextView) row.findViewById(R.id.txtRecord);
		switch(uiStatus){
		case TaskDomain.TaskUIStatus.NONE:
//			Log.d("DEBUG", "uiStatus = NONE");
			btnDelete.setVisibility(View.VISIBLE);
			btnFinish.setVisibility(View.GONE);
			txtRecord.setBackgroundColor(0xFF33B5E5);
			task.setUiStatus(TaskDomain.TaskUIStatus.DELETE);
			break;
		case TaskDomain.TaskUIStatus.FINISH:
//			Log.d("DEBUG", "uiStatus = FINISH");
			btnDelete.setVisibility(View.GONE);
			btnFinish.setVisibility(View.GONE);
			task.setUiStatus(TaskDomain.TaskUIStatus.NONE);
			break;
		case TaskDomain.TaskUIStatus.DELETE:
//			Log.d("DEBUG", "uiStatus = DELETE");
			break;
		}
	}

	@Override
	public void startSwipeUp(View v, Point fromEvent, Point toEvent) {
//		Log.d("DEBUG", "TaskListViewGestureResponder startSwipeRight");
		
	}

	@Override
	public void startSwipeDown(View v, Point fromEvent, Point toEvent) {
//		Log.d("DEBUG", "TaskListViewGestureResponder startSwipeRight");
	}
	
	@Override
	public void onSwipeRight(View v, Point fromEvent, Point toEvent) {
//		Log.d("DEBUG", "TaskListViewGestureResponder onSwipeRight");
	}

	@Override
	public void onSwipeLeft(View v, Point fromEvent, Point toEvent) {
//		Log.d("DEBUG", "TaskListViewGestureResponder onSwipeLeft");
	}

	@Override
	public void onSwipeUp(View v, Point fromEvent, Point toEvent) {
//		Log.d("DEBUG", "TaskListViewGestureResponder onSwipeUp");
		ListView listView = (ListView) v;
		listView.smoothScrollBy(fromEvent.y - toEvent.y, 100);
	}

	@Override
	public void onSwipeDown(View v, Point fromEvent, Point toEvent) {
//		Log.d("DEBUG", "TaskListViewGestureResponder onSwipeDown");
		ListView listView = (ListView) v;
		int firstPosition = listView.getFirstVisiblePosition();
		if (firstPosition == 0) {
			View firstRow = listView.getChildAt(firstPosition);
			TaskListViewAdapter adapter = (TaskListViewAdapter)listView.getAdapter();
			if (firstRow.getTop() == 0) {
//				Log.d("DEBUG", "OverUpScroll!!!");
				TaskDomain task = (TaskDomain)adapter.getItem(0);
				if(task.getStatus() != TaskDomain.TaskStatus.NEW){
					adapter.insertItem(0);
				}
				
			}
		}
		listView.smoothScrollBy(fromEvent.y - toEvent.y, 100);
	}

}
